package com.example.password.page.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.password.R;
import com.example.password.db.entity.Password;

import java.util.ArrayList;
import java.util.List;

public class PasswordListAdapter extends BaseAdapter {
    private Context context;
    private List<Password> passwordList = new ArrayList<>();

    public PasswordListAdapter(Context context) {
        this.context = context;
    }

    public void refresh(List<Password> passwordList) {
        this.passwordList = passwordList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return passwordList.size();
    }

    @Override
    public Password getItem(int i) {
        return passwordList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    @SuppressLint("ViewHolder")
    public View getView(int i, View v, ViewGroup viewGroup) {
         View view = LayoutInflater.from(context).inflate(R.layout.password_list_item, null);
        ViewHolder holder = new ViewHolder(view);
        holder.tvName.setText(getItem(i).getName());
        holder.tvUpdateTime.setText(getItem(i).getUpdateTime());
        return view;
    }

    public static class ViewHolder {
        TextView tvName;
        TextView tvUpdateTime;

        public ViewHolder(View itemView) {
            this.tvName = itemView.findViewById(R.id.tv_name);
            this.tvUpdateTime = itemView.findViewById(R.id.tv_update_time);
        }
    }
}
